<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 150px;
  height: 150px;
  display: block;

}

#register {
  width: 100%;
  height: 880px;
  //min-height: 200vh; /* 保证高度至少为视口高度 */
  background: url("@/assets/background/snow.jpg");
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  //padding: 20px; /* 给容器留一些内边距，避免边界溢出 */
  //box-sizing: border-box;
}

.son {
  width: 650px;
  height: 880px;
  background-color: rgba(255, 255, 255, 0.75);
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  border-radius: 30px;
  //box-sizing: border-box;
}

.el-input {
  border-radius: 10px;
  width: 250px;
}

.el-col{
  border-radius: 10px;
  width: 250px;
}

.el-select{
  border-radius: 10px;
  width: 250px;
}

.el-form {
  margin: 20px 0 20px 130px;
}

.myStatistic{
  text-align: left !important;
}

.myStatistic >>> .number{
  font-size: 12px !important;
  color: gray !important;
}

.myStatistic >>> .con{
  display: inline-block !important;
}
</style>
<template>
  <div id="register">
    <div class="son">
      <el-form ref="user" :model="user" :rules="rules" label-width="80px">
        <el-form-item label="头像">
          <el-upload
              class="avatar-uploader"
              action="api/user/register/avatar"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item label="性别" style="width: 100%;" prop="sex">
          <el-select v-model="user.sex" placeholder="性别">
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="0"></el-option>
            <el-option label="其他" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="user.birthday"
                            style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="user.phone"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="user.realName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input show-password v-model="user.password"
                    autocomplete="new-password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
          <el-input show-password v-model="user.password2"
                    autocomplete="new-password" @input="opinionPsw"></el-input>
          <span v-if="!passwordFlag" style="color: #F56C6C;font-size: 12px">两次密码不一致</span>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="user.code"></el-input>
          <a @click="sentEmail()" style="cursor: pointer;margin-left: 10px;color: #0491dc" v-if="codeFlag">发送验证码</a>
          <el-statistic
              v-else
              @finish="codeFlag = true"
              format="ss秒后可再次发送验证码"
              :value="deadline"
              time-indices
              class="myStatistic"
          >
          </el-statistic>
        </el-form-item>
        <el-form-item>
          <el-button @click="backHome()" style="margin-left: 30px">取消</el-button>
          <el-button type="primary" @click="onSubmit">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import particlesJson from '@/assets/RegisterParticles.json'
import axios from "@/Axios/axios";

export default {
  data() {
    return {
      user: {
        username: '',
        password: '',
        password2: '',
        sex: '',
        phone: '',
        birthday: '',
        email: '',
        realName: '',
        identity: '',
        headImage: '',
        code:'',
      },
      imageUrl: '',
      deadline: null,
      passwordFlag: true,
      codeFlag: true,
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'change'},
        ],
        birthday: [
          {required: true, message: '请选择生日', trigger: 'change'},
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur'},
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {min: 11, max: 11, message: '请输入正确的长度为十一位的手机号码', trigger: 'blur'},
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
        ],
        realName: [
          {required: true, message: '请输入真实姓名', trigger: 'blur'},
          {min: 2, max: 10, message: '请输入正确的真实姓名格式', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {
            pattern: /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{6,50}$/,
            message: '密码至少为6位数并同时包含大、小写字母、数字和特殊符号(!@#$%^&*)',
            trigger: 'blur'
          }
        ],
        password2: [
          {required: true, message: '请输入确认密码', trigger: 'blur'},
        ],
        confirmPassword: [
          {required: true, message: "请输入确认密码", trigger: 'blur'},
        ],
      }
    }
  },
  mounted() {
    require('particles.js')
// eslint-disable-next-line no-undef
    particlesJS('register', particlesJson);
  },
  methods: {
    opinionPsw() {
      this.passwordFlag = this.user.password2 === this.user.password;
    },

    onSubmit() {
      if (this.user.headImage === '') {
        this.$message({
          message: '请先上传头像再注册哦',
          type: 'warning'
        });
        return;
      }
      axios.post('/user/register', this.user).then(res => {
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '注册成功,请登录'
          })
          this.$router.push('/login');
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      }).catch(() => {})
    },

    backHome() {
      this.$router.push('/');
    },

    handleAvatarSuccess(res) {
      if (res.code !== 200) {
        this.$message.error(res.msg);
      }
      this.imageUrl = res.data.url;
      this.user.headImage = res.data.groupId;
    },

    beforeAvatarUpload(file) {
      const imageFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp'];
      const isImage = imageFormats.includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isImage) {
        this.$message.error('上传头像图片只能是图片格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isImage && isLt2M;
    },
    sentEmail(){
      let email = this.user.email;
      if(email == null || email === '' || email === undefined){
        this.$message.warning("请先输入邮箱！")
        return;
      }
      this.codeFlag = false;
      this.deadline =  Date.now() + 1000 * 60 ;
      axios.get('/system/sendMail/'+ email).then(res => {
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: res.msg
          });
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
      }).catch(() =>{})
    },
  },
  created() {

  }
}
</script>